package com.java28.rbac.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java28.rbac.pojo.Role;
import com.java28.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("index")
    public String roleView(){
        return "WEB-INF/role/role";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> list(Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "5")Integer pageSize,String like){
        Map<String,Object> map = new HashMap<String,Object>();
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Role> roleList = roleService.findAllByLike(like);
            PageInfo<Role> pageInfo = new PageInfo<Role>(roleList);
            map.put("roleList",roleList);
            map.put("pageInfo",pageInfo);
            map.put("result",true);
        }catch(Exception e){
            map.put("result",false);
        }

        return map;
    }

}
